package cn.ecnu.controller.resume;

import cn.ecnu.common.utils.R;
import cn.ecnu.common.utils.TencentCloudUtil;
import cn.ecnu.common.utils.ThreadCandidate;
import cn.ecnu.common.validated.InsertGroup;
import cn.ecnu.common.validated.UpdateGroup;
import cn.ecnu.common.validated.ValidList;
import cn.ecnu.pojo.personal.Title;
import cn.ecnu.pojo.resume.*;
import cn.ecnu.service.match.MatchCandidateService;
import cn.ecnu.service.personal.TitleService;
import cn.ecnu.service.resume.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Validated
@RequestMapping("/resume")
public class ResumeController2 {

    @Resource
    ResumeBasicService resumeBasicService;
    @Resource
    ResumeEduService resumeEduService;
    @Resource
    ResumeExpService resumeExpService;
    @Resource
    ResumeExtraService resumeExtraService;
    @Resource
    ResumeJobService resumeJobService;
    @Resource
    ResumeSkillService resumeSkillService;
    @Resource
    MatchCandidateService matchCandidateService;

    @PostMapping("/basic")
    public R saveResumeBasic(@Validated @RequestBody ResumeBasic resumeBasic){
        if (resumeBasicService.saveOrUpdate(resumeBasic)) {
            return R.ok();
        }
        return R.error("简历-基本信息更新失败");
    }

    @GetMapping("/basic")
    public R getResumeBasic(@RequestParam("userId") Integer userId){
        ResumeBasic result = resumeBasicService.getById(userId);
        if (result == null){
            return R.error("基本信息查询错误");
        }
        return R.data(result);
    }



//    @PostMapping("/edu")
//    public R insertResumeEdu(@Validated(InsertGroup.class) @RequestBody ResumeEdu resumeEdu) {
//        if (resumeEduService.save(resumeEdu)){
//            return R.ok();
//        }
//        return R.error("简历-教育经历添加失败");
//    }

    @PostMapping("/edu")
    public R saveResumeEdu(@Validated @RequestBody ValidList<ResumeEdu> resumeEdus){
        if (!resumeEduService.saveOrUpdateBatch(resumeEdus)) {
            return R.error("简历-教育经历更新失败");
        }
        return R.ok();
    }

    @GetMapping("/edu")
    public R queryResumeEdu(@RequestParam("userId")Integer userId){
        List<ResumeEdu> resumeEdus;
        if ((resumeEdus=resumeEduService.list(Wrappers.<ResumeEdu>lambdaQuery()
                .eq(ResumeEdu::getUserId,userId)
                .orderByDesc(ResumeEdu::getEduStart))) == null){
            return R.error("简历-教育经历为空");
        }
        return R.data(resumeEdus);
    }



//    @PostMapping("/exp")
//    public R insertResumeExp(@Validated(InsertGroup.class) @RequestBody ResumeExp resumeExp) {
//        if (resumeExpService.save(resumeExp)) {
//            return R.ok();
//        }
//        return R.error("简历-期望职位添加失败");
//    }
//
//    @PostMapping("/exp")
//    public R saveResumeExp(@Validated @RequestBody ValidList<ResumeExp> resumeExps){
//        if (!resumeExpService.saveOrUpdateBatch(resumeExps)) {
//            return R.error("简历-期望岗位更新失败");
//        }
//        return R.ok();
//    }
//
//    @GetMapping("/exp")
//    public R queryResumeExp(@RequestParam("userId")Integer userId){
//        List<ResumeExp> resumeExps;
//        if ((resumeExps=resumeExpService.list(Wrappers.<ResumeExp>lambdaQuery()
//                .eq(ResumeExp::getUserId,userId))) == null){
//            return R.error("简历-期望岗位为空");
//        }
//        return R.data(resumeExps);
//    }

    @Resource
    TitleService titleService;
    @Resource
    ThreadCandidate threadCandidate;
    @PostMapping("/exp")
    public R saveResumeExp(@Validated @RequestBody ResumeExp resumeExp){
        if (resumeExpService.saveOrUpdate(resumeExp)) {
            threadCandidate.setResumeExp(resumeExp);
            new Thread(threadCandidate).start();
            titleService.update(Wrappers.<Title>lambdaUpdate()
                .eq(Title::getUserId, resumeExp.getUserId())
                .set(Title::getBulletinCandidate, resumeExp.getExpPosition()));
            return R.ok();
        }
        return R.error("简历-基本信息更新失败");
    }

    @GetMapping("/exp")
    public R getResumeExp(@RequestParam("userId") Integer userId){
        ResumeExp result = resumeExpService.getById(userId);
        if (result == null){
            return R.error("基本信息查询错误");
        }
        return R.data(result);
    }



//    @PostMapping("/extra")
//    public R insertResumeExtra(@Validated(InsertGroup.class) @RequestBody ResumeExtra resumeExtra) {
//        if (resumeExtraService.save(resumeExtra)) {
//            return R.ok();
//        }
//        return R.error("简历-在校经历添加失败");
//    }

    @PostMapping("/extra")
    public R saveResumeExtra(@Validated @RequestBody ValidList<ResumeExtra> resumeExtras){
        if (!resumeExtraService.saveOrUpdateBatch(resumeExtras)) {
            return R.error("简历-在校经历更新失败");
        }
        return R.ok();
    }

    @GetMapping("/extra")
    public R queryResumeExtra(@RequestParam("userId")Integer userId){
        List<ResumeExtra> resumeExtras;
        if ((resumeExtras=resumeExtraService.list(Wrappers.<ResumeExtra>lambdaQuery()
                .eq(ResumeExtra::getUserId,userId))) == null){
            return R.error("简历-在校经历为空");
        }
        return R.data(resumeExtras);
    }



//    @PostMapping("/job")
//    public R insertResumeJob(@Validated(InsertGroup.class) @RequestBody ResumeJob resumeJob) {
//        if (resumeJobService.save(resumeJob)) {
//            return R.ok();
//        }
//        return R.error("简历-工作经历添加失败");
//    }

    @PostMapping("/job")
    public R saveResumeJob(@Validated/*(UpdateGroup.class)*/ @RequestBody ValidList<ResumeJob> resumeJobs){
        if (!resumeJobService.saveOrUpdateBatch(resumeJobs)) {
            return R.error("简历-工作经历更新失败");
        }
        return R.ok();
    }

    @GetMapping("/job")
    public R queryResumeJob(@RequestParam("userId")Integer userId){
        List<ResumeJob> resumeJobs;
        if ((resumeJobs=resumeJobService.list(Wrappers.<ResumeJob>lambdaQuery()
                .eq(ResumeJob::getUserId,userId)
                .orderByDesc(ResumeJob::getJobStart))) == null){
            return R.error("简历-工作经历为空");
        }
        return R.data(resumeJobs);
    }



//    @PostMapping("/skill")
//    public R insertResumeSkill(@Validated(InsertGroup.class) @RequestBody ResumeSkill resumeSkill) {
//        if (resumeSkillService.save(resumeSkill)) {
//            return R.ok();
//        }
//        return R.error("简历-职业技能添加失败");
//    }

    @PostMapping("/skill")
    public R saveResumeSkill(@Validated/*(UpdateGroup.class)*/ @RequestBody ValidList<ResumeSkill> resumeSkills){
        if (!resumeSkillService.saveOrUpdateBatch(resumeSkills)) {
            return R.error("简历-职业技能更新失败");
        }
        return R.ok();
    }

    @GetMapping("/skill")
    public R queryResumeSkill(@RequestParam("userId")Integer userId){
        List<ResumeSkill> resumeSkills;
        if ((resumeSkills=resumeSkillService.list(Wrappers.<ResumeSkill>lambdaQuery()
                .eq(ResumeSkill::getUserId,userId))) == null){
            return R.error("简历-职业技能为空");
        }
        return R.data(resumeSkills);
    }
}
